/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.order.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.order.dto.PurchaseOrderDetail;

public class PurchaseOrderDetailDAOHibernate extends HibernateDaoSupport implements PurchaseOrderDetailDAO {

	@SuppressWarnings("unchecked")
	public PurchaseOrderDetail getPurchaseOrderDetail(String orderId, int productId) {
		String queryString = "from PurchaseOrderDetail d where d.order.id = '" + orderId + "' and productId=" + productId;
		List<PurchaseOrderDetail> result = getHibernateTemplate().find(queryString);
		return result.get(0);
	}
	
	public void savePurchaseOrderDetail(PurchaseOrderDetail detail) {
		getHibernateTemplate().saveOrUpdate(detail);
	}
	
	@SuppressWarnings("unchecked")
	public long getCountByProductId(int productId) {
		String sql = "select count(d.id) from PurchaseOrderDetail d where d.productId=?";
		List result = getHibernateTemplate().find(sql, productId);
		long count = 0;
		if (result.size() > 0) {
			count = (Long)result.get(0);
		}
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public long getCountByUnitId(int unitId) {
		String sql = "select count(d.id) from PurchaseOrderDetail d where d.unit=?";
		List result = getHibernateTemplate().find(sql, String.valueOf(unitId));
		long count = 0;
		if (result.size() > 0) {
			count = (Long)result.get(0);
		}
		return count;
	}

}
